package com.waterworld.watch.home.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.waterworld.watch.R;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.AddressBookBean;
import com.waterworld.watch.common.bean.UserInfoBean;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.ToastUtils;
import com.waterworld.watch.home.adapter.SingleListenAdapter;
import com.waterworld.watch.home.interfaces.IHomeManager;
import com.waterworld.watch.home.manager.HomeManager;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SkillSingleListenerActivity extends BaseActivity{

    @BindView(R.id.header_parent)
    LinearLayout headerParent;

    @BindView(R.id.header_title)
    TextView headerTitle;

    @BindView(R.id.header_back)
    ImageView headerBack;

    @BindView(R.id.rv_list)
    RecyclerView rv_list;

    @BindView(R.id.btn_listener)
    Button btn_listener;

    private IHomeManager iHomeManager = HomeManager.getInstance();
    private List<AddressBookBean> mList = new ArrayList<>();
    private SingleListenAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_listener);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        setViewSize(headerParent, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this) / 10);
        headerTitle.setText(getString(R.string.one_way_listen));
        headerBack.setVisibility(View.VISIBLE);
        headerTitle.setVisibility(View.VISIBLE);
    }

    private void initData() {
        if (iHomeManager != null) {
            iHomeManager.listOneWayListeningUser(new BaseObserverListener<BaseResultBean<AddressBookBean[]>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(BaseResultBean<AddressBookBean[]> baseResultBean) {
                    if (baseResultBean.getCode() == 0) {
                        if (baseResultBean.getData() != null) {
                            mList.addAll(Arrays.asList(baseResultBean.getData()));
                            mAdapter = new SingleListenAdapter(SkillSingleListenerActivity.this,mList);
                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(SkillSingleListenerActivity.this);
                            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                            rv_list.setLayoutManager(linearLayoutManager);
                            rv_list.setAdapter(mAdapter);
                            boolean flag = false;
                            for (int i=0;i<mList.size();i++) {
                                if (mList.get(i).getPhone().equals(UserInfoBean.getInstance().getUser().getPhone())) {
                                    btn_listener.setClickable(true);
                                    flag = true;
                                }
                            }
                            if (!flag) {
                                btn_listener.setClickable(false);
                                btn_listener.setBackground(getResources().getDrawable(R.drawable.upload_button_pressed));
                            }
                        }
                    }
                }
            });
        }
    }

    @OnClick(R.id.btn_listener)
    void onListener() {
        if (iHomeManager != null) {
            iHomeManager.oneWayListening(new BaseObserverListener<BaseResultBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(BaseResultBean baseResultBean) {
                    if (baseResultBean.getCode() == 0) {
                        ToastUtils.showShort(SkillSingleListenerActivity.this,"请耐心等待，手表会主动拨打您的电话~");
                    }
                }
            });
        }
    }
}
